//
//  HX_CollectionLayout.m
//  UI15-网易新闻
//
//  Created by 韩旭 on 16/8/3.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HX_CollectionLayout.h"

@interface HX_CollectionLayout ()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *> *attributesArrays;
@property (nonatomic, assign) CGFloat itemHeight;
@property (nonatomic, assign) CGFloat contentWidth;
@end

@implementation HX_CollectionLayout
- (instancetype)init {
    self = [super init];
    if (self) {
         _attributesArrays = [NSMutableArray array];
    }
    return self;
}
- (CGFloat)itemHeight {
    CGFloat allHeight = CGRectGetHeight(self.collectionView.bounds);
    return allHeight;
}
- (void)prepareLayout {
    CGFloat x = 10;
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *layout = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        CGFloat width = [self.delegate collectionView:self.collectionView layout:self width:self.itemHeight sizeForItemAtIndexPath:index];
        CGFloat y = 0;
        layout.frame = CGRectMake(x, y, width, self.itemHeight);
        x += width + 10;
        x = ceilf(x);
        _contentWidth = x + 10;
        [_attributesArrays addObject:layout];
    }
}


- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _attributesArrays;
}

//设置滚动方向
- (CGSize)collectionViewContentSize {
    return CGSizeMake(_contentWidth, 0);
}

@end
